// pages/personal/balancerecharge/balancerecharge.js
var utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    animationData: {},
    animationData2: {},
    curHdIndex: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getSystemInfo({
      success(res) {
        that.setData({
          windowHeight: res.windowHeight * 0.26
        })
      }
    })
    this.stretch(this.data.windowHeight);
    this.shrink(100);

    that.get_list();
  },
  onShow: function() {
    var that = this;
    that.personalInfo();
  },
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      console.log(res);
      that.setData({
        info: res.datas
      })
    })
  },
  get_list: function(e) {
    var that = this;
    utils.http("/api/v1/recharge/get_list", {}, function(res) {
      for (var i in res.datas) {
        if ((parseInt(i) + parseInt(1)) % 3 == 1) {
          res.datas[i].sty = 'lan';
        } else if ((parseInt(i) + parseInt(1)) % 3 == 2) {
          res.datas[i].sty = 'fen';
        } else if ((parseInt(i) + parseInt(1)) % 3 == 0) {
          res.datas[i].sty = 'green';
        }
      }

      that.setData({
        recharge: res.datas
      })
    })
  },
  change(e) {
    this.setData({
      current: e.detail.current
    })
    this.stretch(this.data.windowHeight)
    this.shrink(100)
  },
  // 收缩
  stretch(h) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
    this.animation = animation
    animation.height(h).step()
    this.setData({
      animationData: animation.export(),
    })
  },
  // 展开
  shrink(h) {
    var animation2 = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
    this.animation2 = animation2
    animation2.height(h).step()
    this.setData({
      animationData2: animation2.export()
    })
  },

  //充值成功跳转页面
  formSubmit: function(e) {
    var that = this;
    var price = e.detail.value.namePrice;
    if (price != "") {
      utils.showLoading();
      utils.http("/api/v1/member/recharge", {
        total_price: price
      }, function(res) {
        if (res.state == 1) {
          utils.hideLoading();
          wx.requestPayment({
            timeStamp: res.datas.timeStamp,
            nonceStr: res.datas.nonceStr,
            package: res.datas.package,
            signType: res.datas.signType,
            paySign: res.datas.paySign,
            success(res) {
              wx.navigateTo({
                url: '../chargesuccess/chargesuccess?price=' + price,
              })
            },
            fail(res) {}
          })

        } else {
          utils.message(res.datas);
        }
      })
    }


  },
  //开通会员页面
  onOpenVipTap: function(e) {
    var that = this;
    that.setData({
      ishidden: true
    })
    wx.navigateTo({
      url: '../../openvip/openvip'
    })
  },

  //金额切换
  tab: function(e) {
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    this.setData({
      curHdIndex: id,
      price: price
    })
  }
})